package allove.eric.oeditor;
/* by Allove Eric Sun @ 20091212
 *  contact me with eric.sunjian@gmail.com
 * this file is used for choose the file or directory 
 * which is for editing or saving usage !
 * */
import java.io.File;
import java.util.ArrayList;

import allove.eric.oeditor.common.FinalUtils;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class FileChooser extends ListActivity implements OnItemClickListener , OnItemSelectedListener , OnClickListener {
	
	private final String TAG = "FileChooser" ;
	private final boolean debug = FinalUtils.isDebug ;
	private ArrayList<String> fileNames ;
	private ArrayList<String> filePaths ;
	private final String SD_ROOT_1 = "/sdcard" ;
	private final String SD_ROOT_2 = "/sdcard/" ;
	private final String FLAG_RETURN_SD_ROOT = "_return_sd" ;
	private final String FLAG_RETURN_PRE_DIR = "_return_pre_dir" ;
	private TextView curFilePath  ;
	private Button chooseOK ;
	private ListView listView ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.file_choose_list_activity) ;
		this.initView() ;
		Bundle b = this.getIntent().getExtras() ;		
		if(b != null){
			int forWhat = b.getInt(HiEditor.KEY_FOR_WHAT) ;
			if(forWhat == HiEditor.FOR_SAVE){
				this.chooseOK.setEnabled(true) ;
			} else {
				this.chooseOK.setEnabled(false) ;
			} 
		}		
		this.listView.setOnItemClickListener(this) ;
		this.listView.setOnItemSelectedListener(this) ;
		this.getDirList(SD_ROOT_1) ;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// land
		} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			// port
		}
	}
	
	private void initView(){
		this.listView = this.getListView() ;
		this.curFilePath = (TextView) this.findViewById(R.id.file_chooser_tips) ;
		this.chooseOK = (Button)this.findViewById(R.id.choose_ok) ;
		this.chooseOK.setOnClickListener(this) ;
	}
	
	private void getDirList(String path) {
		this.fileNames = new ArrayList<String>() ;
		this.filePaths = new ArrayList<String>() ;
		this.curFilePath.setText(path) ;
		File f = new File(path) ;
		File[] list = f.listFiles() ;
		
		if(!path.equals(this.SD_ROOT_1) && !path.equals(this.SD_ROOT_2)){
			/** add the root flag*/
			this.fileNames.add(this.FLAG_RETURN_SD_ROOT);
			this.filePaths.add(this.SD_ROOT_1) ;
			
			/** add the parent flag*/
			this.fileNames.add(this.FLAG_RETURN_PRE_DIR) ;
			this.filePaths.add(f.getParent()) ;
		}
		
		int size = list.length ;
		for(int i = 0 ; i < size ; i++){
			this.fileNames.add(list[i].getName()) ;
			if(debug) Log.d(TAG, "filename = " + list[i].getName())  ;
			this.filePaths.add(list[i].getPath()) ;
			if(debug) Log.d(TAG, "filepath = " + list[i].getPath())  ;
		}
		
		this.getListView().setAdapter(new FileDirAdapter(this,this.fileNames,this.filePaths)) ;
	}
	
	private class FileDirAdapter extends BaseAdapter {

		ArrayList<String> names ;
		ArrayList<String> paths ;
		LayoutInflater inflater ;
		
		public FileDirAdapter(Context ctx , ArrayList<String> fileNames , ArrayList<String> filePaths){
			this.inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
			this.names = fileNames ;
			this.paths = filePaths ;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return this.names.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position ;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return  position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder ;
			if(convertView == null){
				holder = new ViewHolder() ;
				convertView = (LinearLayout) this.inflater.inflate(R.layout.file_chooser_list_item, null) ;
				holder.fileName = (TextView)convertView.findViewById(R.id.file_name) ;
				convertView.setTag(holder) ;
			} else {
				holder = (ViewHolder)convertView.getTag() ;
			}
			String current = this.names.get(position) ;
			if(current != null){
				if(current.equals(FLAG_RETURN_SD_ROOT)){
					holder.fileName.setText("返回根目录") ;
				} else if(current.equals(FLAG_RETURN_PRE_DIR)){
					holder.fileName.setText("返回上级目录") ;
				} else {
					File f = new File(current) ;
					holder.fileName.setText(f.getName()) ;
					if(f.isDirectory()){
						
					} else {
						
					}
				}
			}		
			
			return convertView;
		}
		
		class ViewHolder {
			TextView fileName ;
		}
		
	}

	@Override
	public void onItemClick(AdapterView<?> adpterView, View view, int position, long rowId) {
		// TODO Auto-generated method stub
		String curPath = this.filePaths.get(position) ;
		if(curPath != null){
			File f = new File(curPath) ;
			if(f.isDirectory()){
				this.getDirList(curPath) ;
			} else {
				// not a dir , return file path and name directly !
				Bundle b = new Bundle() ;
				b.putString(HiEditor.KEY_FILE_PATH, f.getPath()) ;
				Intent i = new Intent();
				i.setClass(FileChooser.this, HiEditor.class) ;
				i.putExtras(b) ;
				setResult(RESULT_OK , i) ;
				finish() ;
			}
		} else {
			if(debug) Log.d(TAG, "curPath is null ") ;
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		// TODO Auto-generated method stub
		String name = this.fileNames.get(position) ;
		if(!name.equals(FLAG_RETURN_PRE_DIR) && !name.equals(FLAG_RETURN_SD_ROOT)){
			this.curFilePath.setText(this.filePaths.get(position)) ;
		}	
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.choose_ok : {
			
		} ; break ;
		}
	}

}
